package com.wx.activitys.admin.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.wx.activitys.base.utils.Common;
import com.wx.activitys.drp.model.BPlatfrom;
import com.wx.activitys.drp.service.PlatfromService;

@Controller
@RequestMapping("/platfrom")
public class PlatfromController {
	
	
	@Autowired
	private PlatfromService platfromService;


	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) {
		//String pathRoot = request.getSession().getServletContext().getRealPath(""); 
		getList(request,model);
		return "admin/platfromManage/list";
	}
	
	 private void getList(HttpServletRequest request, Model model) {
			BPlatfrom entity = new BPlatfrom();
			entity.getMap().put("pageIndex", 0);
			entity.getMap().put("pageSize", 15);
			List<BPlatfrom> platfromList = platfromService.queryPageList(entity);
			int page_all = platfromService.selectCount(entity);
			model.addAttribute("List", platfromList);
			model.addAttribute("page_all", (int)(Math.ceil(((double)page_all/10))));
	 }
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model, HttpServletRequest request) {
		return "admin/platfromManage/add";
	}
	@RequestMapping(value = "remove", method = RequestMethod.GET)
	public String delete(Model model, HttpServletRequest request,String id) {
		BPlatfrom entity = new BPlatfrom();
		entity.setId(id);
		platfromService.delete(entity);
		getList(request,model);
		return "admin/platfromManage/list";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model, HttpServletRequest request,String id) {
		BPlatfrom platfrom = new BPlatfrom();
		platfrom.setId(id);
		BPlatfrom entity = platfromService.selectEntity(platfrom);
		model.addAttribute("entity", entity);
		return "admin/platfromManage/add";
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, HttpServletRequest request,BPlatfrom entity) {
		int result;
		if(entity.getId().isEmpty()){
		    result = platfromService.insert(entity);
		}else{
			result = platfromService.update(entity);
		}
		
		getList(request,model);
		return "admin/platfromManage/list";
	}
	
    @RequestMapping(value="/upload",method=RequestMethod.POST)  
    @ResponseBody
    private String fildUpload(@RequestParam(value="file",required=false) MultipartFile file,  
            HttpServletRequest request)throws Exception{  
          
        //获得物理路径webapp所在路径  
        //String pathRoot = request.getSession().getServletContext().getRealPath("");  
        String pathRoot = Common.IMG_SAVE_BASEPATH + "icon/";
        
        
        File file1 = null;  
        try {  
            file1 = new File(pathRoot);  
            if (!file1.exists()) {  
                 file1.mkdirs();  
            }  
            
        } catch (Exception e) {  
        }  
        
        String path="";  
        if(!file.isEmpty()){  
        	
            //生成uuid作为文件名称  
            String uuid = UUID.randomUUID().toString().replaceAll("-","");  
            //获得文件类型（可以判断如果不是图片，禁止上传）  
            String contentType=file.getContentType();  
            //获得文件后缀名称  
            String imageName=contentType.substring(contentType.indexOf("/")+1);  
//            path="/static/images/"+uuid+"."+imageName;  
            path=uuid+"."+imageName;  
            file.transferTo(new File(pathRoot+"/"+path));  
        }  
        System.out.println(path);  
        request.setAttribute("imagesPath", "icon/"+path);  
        return "icon/"+path;  
    }  
    
}
